﻿
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmoneyWeiXinCommon
{
    public class Menu
    {
        #region 创建菜单
        public static int Create(string menuJson, string appid, string appsercret)
        {
            int funcRet = -9999;
            string token = TokenHelper.GetToken(appid, appsercret);
            if (string.IsNullOrEmpty(token))
            {
                return funcRet;
            }

            Dictionary<string, string> urlParams = new Dictionary<string, string>();
            urlParams.Add("access_token", token);
            string url = Config.WeiXin_ApiUrl + "menu/create";
            string ret = NetHelper.LoadDataPost(url, urlParams, menuJson, Encoding.UTF8);

            //add
            if (ret.IndexOf("{\"errcode\":40001") != -1)
            {
                TokenHelper.ClearToken(appid);
                urlParams = new Dictionary<string, string>();
                urlParams.Add("access_token", TokenHelper.GetToken(appid, appsercret));
                ret = NetHelper.LoadDataPost(url, urlParams, menuJson, Encoding.UTF8);
            }
            //MyTrace.get().add("CreateMenu.end=[" + ret + "]");
            JObject jObj = JObject.Parse(ret);
            if (jObj != null)
            {
                JToken jToken;
                if (jObj.TryGetValue("errcode", out jToken))
                {
                    string errcode = jObj["errcode"].ToString().Replace("\"", "");
                    funcRet = int.Parse(errcode);
                }
            }
            return funcRet;
        }
        #endregion




    }
}
